<template>
	<view class="body">
		<!-- 订单标题 -->
		<view class="top">
				<text class="fk">{{title}}</text>
			</view>
			<!-- 订单内容 -->
			<view class="order-conent">
				<view>
					<text class="order-id">订单编号:</text>
					<text class="idtext">{{order.id}}</text>
					<text class="order-id-fz" @click="copy" >复制</text> 
				</view>
				<view>
					<text class="order-user">创建用户:</text>
					<text class="username">{{order.issueid}}</text>
				</view>
				<view>
					<text class="order-time">下单时间:</text>
					<text class="createtime">{{order.createTime}}</text>
				</view>
				<view>
					<text class="zf-time">订单总价:</text>
					<text class="zftype" :style="{color:flage?'#3c9cff':'#ff5500'}">{{order.price}}</text>
				</view> 
				<view>  
					<text class="zf-time">是否支付:</text>
					<text class="zftype" :style="{color:flage?'#3c9cff':'#ff5500'}">{{flagetext}}</text>
				</view> 
				<view class="orderjiesao">
					<text class="js">订单内容:</text>
				</view>
				<!-- 订单包含商品信息 -->
				<view class="orderList" v-for="(item,index) in order.orderList" :key="index">
					<view class="order-card" @click="ordercontentclick(index)">
						<view class="ordercardimg">
							<u-avatar :src="item.indeximg" size="120rpx"></u-avatar>
						</view>
						<!-- 订单标题 -->
						<view class="order-title">
							<text class="text">{{item.title}}</text>
						</view>
						<!-- 订单介绍 -->
						<view class="order-content">
							<text class="contenttext">{{item.content}}</text>
						</view>
						<!-- 订单数量 -->
						<view class="order-num">
							<text class="numtext">数量:   x {{item.buynum}}</text>
						</view>
						<!-- 订单价格 -->
						<view class="order-price">
							<text class="pricetext">{{item.totalprice}}</text>
						</view>
					</view>
					<view class="line" style="margin-top: -16rpx;margin-left: 16rpx; margin-right: 16rpx;">
						<u-line color="#a6a9af"></u-line>
					</view>
				</view>
				<!-- 地址信息 -->
				<view class="address">
					<view class="contacts">
						<text class="contactstext">联系人:</text>
						<text class="contactscontent">{{order.buyname}}</text>
					</view>
					<!-- 联系方式 -->
					<view class="contactsphone">
						<text class="contactstext">联系电话:</text>
						<text class="phone">{{order.buyphone}}</text>
					</view>
					<!-- 地址信息 -->
					<view class="contactsaddress">
						<text class="contactstext">联系地址:</text>
						<text class="contactadrs">{{order.buyaddress}}</text>
					</view>
				</view>
			</view>
			<!-- 立即付款 -->
			<view class="fkbutton" @click="fkbutton" v-show="s">
				<text class="fkbutton1" v-if="order.state == 0">立即付款</text>
				<text class="fkbutton2" v-if="order.state == 1">已付款</text>
			</view>
		</view>
</template>

<script>
	import {http} from "../static/service/requestAddress/rtas.js"
	export default {
		data() {
			return {
				isPaying: false, // 是否正在支付 
				s:true,//支付键
				title:"未付款",//订单头部是否付款文字
				flage:false,//是否支付标记
				flagetext:"未支付",//是否支付文字
				order: {
					id:"",//订单id
					buyname:"",//联系人
					buyphone:"",//联系人电话
					createTime:"",//订单创建时间
					buyaddress:"",//地址
					orderList:[],//商品数组
					title:"", //标题
					issueid:"",//创建用户id
					ordertypeid:"助农服务",//订单所属类型id
					state:0,//订单状况
					buynum:0	,//商品购买个数
					price:0,//订单总价
				} 
			}
		},
		onShow() {
			this.isPaying = false; // 无论成功还是失败，都重新启用按钮
		},
		methods: {
			onLoad(options){
				console.log("订单数据是:"+JSON.stringify(options))
				var data = JSON.parse(options.data)
				console.log("对象："+ data.orderlist)
				this.order.id = data.id,
				this.order.buyname = data.buyname,
				this.order.buyphone = data.buyphone,
				this.order.createTime = data.createTime,
				this.order.buyaddress = data.buyaddress,
				this.order.orderList = JSON.parse(data.orderlist),
				this.order.title = data.title, 
				this.order.issueid = data.issueid,
				this.order.ordertypeid = "助农服务",
				this.order.state = data.state,
				this.order.buynum =data.buynum,
				this.order.price = data.price
				if(this.order.state == 0){
					this.title = "未付款"
				}else if(this.order.state == 1){
					this.title = "已付款"
					this.flagetext = "已支付"
				}else if(this.order.state == 5||this.order.state == 3){
					this.s = false
					this.flagetext = "已结束"
					this.title = "已结束"
				}
			},
			// 复制订单
			copy(){
				 uni.setClipboardData({
				    data:this.order.orderid+"",
				    success: function (res) {
				        uni.getClipboardData({
				        success: function (res) {
				            uni.showToast({
				                title: "复制成功",
				                icon: 'none',
				            })
				        },
				        })
				    },
				    })
			},
			//点击订单详情事件
			ordercontentclick(index){
				var id = this.order.orderList[index].goodsid
				uni.navigateTo({
					url:"/nongsuke/nongsukeInfo?data="+id
				})
			},
			//付款点击事件
			fkbutton(){
				if (this.isPaying) {
				        // 如果正在支付，则不执行任何操作  
				        return;  
				      }  
				  
				this.isPaying = true; // 标记为正在支付 
				uni.showLoading({
				title: '正在拉起支付中'
				})
				//封装请求对象
				var wxp = {
					//订单id
					orderid: this.order.id,
					//获取的openid
					openid:"",
					//订单名称
					 name:this.order.title,
					//订单价格
					 price:this.order.price
				}
				var user = JSON.parse(uni.getStorageSync('user'))
				var url = http+"nongsukellid/buystate/"
				uni.login({
					success(res) {
						uni.request({
							url: http+"wxlogin/"+res.code,
							method:"GET",
							success(res) {
								var openid = res.data.date
								wxp.openid = openid
								uni.request({
									url:http+"wxpay",
									data: wxp, 
									method:"POST",
									success(res) {
										var data = res.data.date
										var orderid = data.orderid.slice(0,19)
										uni.requestPayment({
											// "provider": data.provider,    //支付类型-固定值
											"timeStamp": data.timeStamp, // 时间戳（单位：秒）
											"nonceStr": data.nonceStr, // 随机字符串
											"package": data.packAge, // 固定值
											"signType": data.signType, //固定值
											"paySign": data.paySign, //签名
											    success(res) {
													//支付成功后修改订单状态
													console.log("请求的链接是:"+ url+orderid+"/"+user.id,)
													uni.request({
														url: url+orderid+"/"+user.id,
														method:"PUT",
														complete:(res) =>{
															if(res.data.code == 20041){
																uni.$u.toast("支付成功！")
																//更新购物车缓存
																var cartlist = []
																uni.setStorageSync("cartlist",JSON.stringify(cartlist))
																setTimeout(()=>{
																	//支持成功后跳转到已支付订单里
																	uni.redirectTo({
																		url: "/myorder/order"
																	})
																}, 1000);
																	this.isPaying = false; // 无论成功还是失败，都重新启用按钮
															}else{
																console.log("修改失败")
																	this.isPaying = false; // 无论成功还是失败，都重新启用按钮
															}
														}
													})
												}, 
											    fail(e) {
														this.isPaying = false; // 无论成功还是失败，都重新启用按钮
													uni.$u.toast("支付失败！")
													console.log("支付失败！"+url)
												}
										})
									},
									fail(res) {
											this.isPaying = false; // 无论成功还是失败，都重新启用按钮
										uni.$u.toast("获取openid失败！")
									}
								})
							},
							fail(res) {
									this.isPaying = false; // 无论成功还是失败，都重新启用按钮
								uni.$u.toast("获取code失败！")
							}
						})
					},
					fail(res) {
							this.isPaying = false; // 无论成功还是失败，都重新启用按钮
						uni.$u.toast("获取登录信息失败！")
					}
				})
			},
		},
	}
</script>

<style>
	@import url("../static/css/componentsCss/cart/cartAllInfo.css");
</style>